<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card class="box-card">
      <el-row>
        <el-col>
          <el-button type="primary" @click="openAddCateDiolog">新增分类</el-button>
        </el-col>
      </el-row>
      <!-- 列表 -->
      <table-tree
        :data="cateList"
        :columns="columns"
        show-index
        border
        :tree-type="true"
        :expand-type="false"
        :selection-type="false"
      >
        <template v-slot:isOk="scope">
          <i v-if="scope.row.cat_deleted" class="el-icon-error" style="color:red;font-size: 20px;"></i>
          <i
            style="color:lightgreen;font-size: 20px;"
            v-else
            class="el-icon-success"
            type="success"
          ></i>
        </template>
        <template v-slot:level="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level === 1">二级</el-tag>
          <el-tag type="warning" v-else>三级</el-tag>
        </template>
        <template #control="scope" width="100">
          <el-button type="primary" class="el-icon-edit" circle size="mini"
          @click="openAddCateDiolog"></el-button>
          <el-button type="danger" class="el-icon-delete" circle size="mini" @click="deleteCate(scope.row)"></el-button>
        </template>
      </table-tree>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>

      <!-- 添加分类对话框 -->
      <el-dialog
        title="添加分类"
        :visible.sync="addCateDialogVisible"
        width="30%"
        @close="closeAddCateForm"
      >
        <!-- 内容主体区域 -->
        <el-form
          :model="addCateForm"
          :rules="addCateFormRules"
          ref="addCateFormRef"
          label-width="100px"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addCateForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
              <el-cascader
                v-model="selectedCateList"
                :options="parentCateList"
                :props="cascaderprops"
                @change="handleChange"
                clearable
              ></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="closeAddCateForm">取 消</el-button>
          <el-button type="primary" @click="addCateBtn">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // cate页面需求
      queryInfo: {
        type: [1, 2, 3],
        pagenum: 1,
        pagesize: 5
      },
      // 用户数据
      cateList: [],
      total: null,
      // 表格配置
      columns: [
        {
          label: '列表名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isOk'
        },
        {
          label: '排序',
          type: 'template',
          template: 'level'
        },
        {
          label: '操作',
          type: 'template',
          template: 'control',
          width: '200px'
        }
      ],
      //   添加分类对话框
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      //   添加对话框开关
      addCateDialogVisible: false,
      //   添加对话框规则
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入用户名称', trigger: 'blur' }
        ]
      },
      // 级联选择
      parentCateList: [],
      // 配置
      cascaderprops: {
        expandTrigger: 'hover',
        checkStrictly: true,
        value: 'cat_id',
        label: 'cat_name'
      },
      // 已选择分类
      selectedCateList: []
    }
  },
  mounted () {
    this.getCateList()
  },
  methods: {
    // 获取商品分类列表
    async getCateList () {
      const res = await this.$http.get('categories', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        this.$message({
          message: res.meta.msg,
          type: 'error',
          duration: 1000
        })
        return
      }
      this.cateList = res.data.result
      this.total = res.data.total
    },
    //   分页改变
    handleSizeChange (newsize) {
      this.queryInfo.pagesize = newsize
      this.getCateList()
    },
    // 当前分页
    handleCurrentChange (newpage) {
      this.queryInfo.pagenum = newpage
      this.getCateList()
    },
    // 获取父级选择列表
    async getParentCateList () {
      const res = await this.$http.get('categories', { params: { type: 2 } })
      this.parentCateList = res.data
    },
    // 打开对画框
    async openAddCateDiolog () {
      this.addCateDialogVisible = true
      this.getParentCateList()
    },
    // 选择分类
    handleChange () {
      if (this.selectedCateList.length === 0) {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cate_level = 0
      } else {
        this.addCateForm.cat_pid = this.selectedCateList[this.selectedCateList.length - 1]
        this.addCateForm.cate_level = this.selectedCateList.length
      }
    },
    // 确认添加分类
    addCateBtn () {
      this.$refs.addCateFormRef.validate(async valid => {
        if (!valid) {
          return this.$message({
            type: 'error',
            message: '请确认分类内容'
          })
        }
        const res = await this.$http.post('categories', this.addCateForm)
        console.log(res)
        if (res.meta.status !== 201) {
          return this.$message({
            type: 'error',
            message: res.status.msg
          })
        }
        this.$message({
          type: 'success',
          message: res.status.msg
        })
        this.getCateList()
        this.addCateDialogVisible = false
      })
    },
    closeAddCateForm () {
      this.$refs.addCateFormRef.resetFields()
      this.addCateForm.cat_pid = 0
      this.addCateForm.cate_level = 0
      this.selectedCateList = []
      this.addCateDialogVisible = false
    },
    // 删除分类
    async deleteCate (cateInfo) {
      const res = await this.$http.delete('categories/' + cateInfo.cat_id)
      if (res.meta.status !== 200) {
        return this.$message({
          type: 'error',
          message: res.meta.msg
        })
      }
      this.$message({
        type: 'success',
        message: res.meta.msg
      })

      this.getCateList()
    }

  }
}
</script>

<style lang="less" scoped>
.zk-table {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
